package com.sgb.kjwl.view.ui.fragment.manage.special;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.sgb.capital.view.widget.BaseRecyclerViewAdapterHelper.BaseQuickAdapter;
import com.sgb.capital.view.widget.BaseRecyclerViewAdapterHelper.listener.OnItemChildClickListener;
import com.sgb.capital.view.widget.BaseRecyclerViewAdapterHelper.listener.OnItemClickListener;
import com.sgb.capital.view.widget.MToast;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentSpecialMenberNewBinding;
import com.sgb.kjwl.model.entity.manage.AgentStaffCheckEntity;
import com.sgb.kjwl.view.constant.RouterKey;
import com.sgb.kjwl.view.ui.activity.manage.AgentStaffCheckAddActivity;
import com.sgb.kjwl.view.ui.activity.manage.AgentStaffCheckDetailActivity;
import com.sgb.kjwl.view.ui.activity.manage.special.SpecialMenberNewDetailActivity;
import com.sgb.kjwl.view.ui.adapter.manage.SpecialMenberNewAdapter;
import com.sgb.kjwl.viewmodel.manage.SpecialMenBerNewFragmentModel;
import com.sgb.link.view.widget.InputDialog;

import java.util.ArrayList;
import java.util.List;

public class SpecialMenberNewFragment  extends AppLazyFragment implements OnRefreshListener, OnLoadMoreListener {
    FragmentSpecialMenberNewBinding mBinding;
    SpecialMenBerNewFragmentModel mViewModel;
    private  SpecialMenberNewAdapter adapter;
    private List<AgentStaffCheckEntity> mlist= new ArrayList<>();
    private int type;
    private InputDialog inputDialog;
    private int page=1;
    private int limit=10;
    private int index=-1;

    public static SpecialMenberNewFragment newInstance(int type) {
        SpecialMenberNewFragment fragment = new SpecialMenberNewFragment();
        Bundle args = new Bundle();
        args.putInt(RouterKey.TYPE, type);
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    protected void onLazyLoad() {

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_special_menber_new, container, false);
        mBinding.setLifecycleOwner(this);
        return mBinding.getRoot();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel=new ViewModelProvider(getActivity()).get(SpecialMenBerNewFragmentModel.class);
    }

    @Override
    protected void initView() {
        mlist.clear();
        type =getArguments().getInt(RouterKey.TYPE);
        adapter =new SpecialMenberNewAdapter(R.layout.item_specialmenber_new,mlist);
        LinearLayoutManager layoutManager =new LinearLayoutManager(getActivity());
        mBinding.mRecyclerView.setLayoutManager(layoutManager);
        mBinding.mRecyclerView.setAdapter(adapter);
        adapter.setEmptyView(R.layout.layout_empty_view, mBinding.mRecyclerView);
        mBinding.mSmartRefresh.setOnRefreshListener(this);
        mBinding.mSmartRefresh.setOnLoadMoreListener(this);
        adapter.setOnItemClickListener(new com.chad.library.adapter.base.BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(com.chad.library.adapter.base.BaseQuickAdapter adapter, View view, int position) {
                if (mlist.get(position).getStatus()==1){
                    Intent intent =new Intent(getActivity(),AgentStaffCheckDetailActivity.class);
                    intent.putExtra("key_entity",mlist.get(position));
                    startActivity(intent);
                }else {
                    Intent intent =new Intent(getActivity(), SpecialMenberNewDetailActivity.class);
                    intent.putExtra("id",mlist.get(position).getId());
                    startActivity(intent);
                }
            }
        });
        adapter.setOnItemChildClickListener(new com.chad.library.adapter.base.BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(com.chad.library.adapter.base.BaseQuickAdapter adapter, View view, int position) {
                   switch (view.getId()){
                       case R.id.tv_agree:
                           Intent intent =new Intent(getActivity(),AgentStaffCheckAddActivity.class);
                           intent.putExtra("key_entity",mlist.get(position));
                           startActivity(intent);
                           break;
                       case R.id.tv_refuse:
                           showdialog(position);
                           break;
                   }
            }
        });

        mViewModel.getlist(this,page,limit,type+"");
    }
    public void showdialog(int pos){
        if (inputDialog==null){
            inputDialog =new InputDialog(getActivity()).builder().setNegativeButton("")
                    .setPositiveButton("").setEtHint("填写拒绝理由（必填）").setCancelable(false)
                    .setDialogCallBack(new InputDialog.DialogCallBack() {
                        @Override
                        public void onRightClick(String content, Dialog dialog) {
                            if (TextUtils.isEmpty(content)){
                                MToast.showToast(getActivity(),  "请输入拒绝理由");
                            } else {
                                //请求拒绝接
                                index =pos;
                                mViewModel.rejectAdd(getActivity(),mlist.get(pos).getId(),content);
                                dialog.dismiss();
                            }
                        }

                        @Override
                        public void onLeftClick() {

                        }
                    });
            inputDialog.show();
        }
    }
    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        mViewModel.getlist(this,page,limit,type+"");
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page=1;
        mViewModel.getlist(this,page,limit,type+"");

    }
    public void setview(List<AgentStaffCheckEntity> list){
        mBinding.mSmartRefresh.finishRefresh();
        mBinding.mSmartRefresh.finishLoadMore();
        if (page==1){
            mlist.clear();
        }
           mlist.addAll(list);
           adapter.notifyDataSetChanged();
    }
//    public void setAgree(){
//              page=1;
//              mViewModel.getlist(this,page,limit,type+"");
//    }
    public void refreshData(){
        page=1;
        mViewModel.getlist(this,page,limit,type+"");
    }
}
